//
//  HMMiddleView.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/8/9.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit
import SnapKit

class HMMiddleView: UIView {
    
    
    var con:Constraint?
    
    var statusViewModel:HMCellViewModel? {
        
        didSet {
            
            con?.uninstall()
            // 赋值
            textLabel.text = statusViewModel?.retweetedText
            
            if let c = statusViewModel?.status?.retweeted_status?.pic_urls where c.count > 0 {
                
                // 没有原创图
                pictureCollectionView.hidden = false

                self.snp_makeConstraints { (make) in
                    con = make.bottom.equalTo(pictureCollectionView.snp_bottom).offset(HMStatusCellMargin).constraint
                }
                
                // 赋值
                pictureCollectionView.pictData = c
                
            } else {
                
                // 没有原创图 
                pictureCollectionView.hidden = true
                
                self.snp_makeConstraints { (make) in
                    con = make.bottom.equalTo(textLabel.snp_bottom).offset(HMStatusCellMargin).constraint
                }
            }
            
        }
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 239/255, alpha: 1)
        
        addSubview(textLabel)
        addSubview(pictureCollectionView)
        
        textLabel.snp_makeConstraints { (make) in
            
            make.leading.top.equalTo(self).offset(HMStatusCellMargin)
            make.trailing.equalTo(self).offset(-HMStatusCellMargin)
        }
        
        pictureCollectionView.snp_makeConstraints { (make) in
            
            make.leading.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(HMStatusCellMargin)
        }
        
        // 约束整个View的高度等于textLabel的地步
        self.snp_makeConstraints { (make) in
            con = make.bottom.equalTo(textLabel.snp_bottom).offset(HMStatusCellMargin).constraint
        }
        
    }
    
    // MARK: -懒加载
    private lazy var textLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        // 多行
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var pictureCollectionView: HMPictureCollectionView = HMPictureCollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())


}
